package com.yz.xyt.controller;

import com.yz.xyt.constant.Constants;
import com.yz.xyt.domain.LoginUser;
import com.yz.xyt.domain.vo.LeaveVo;
import com.yz.xyt.domain.vo.StudentLeaveExportVo;
import com.yz.xyt.service.ILeaveService;
import com.yz.xyt.service.ILoginService;
import com.yz.xyt.utils.BeanCopyUtils;
import com.yz.xyt.utils.DateUtils;
import com.yz.xyt.utils.ExcelUtil;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 数据导出
 */

@RestController
@RequestMapping("/output")
public class DataOutputController {

    @Autowired
    ILeaveService leaveService;
    @Autowired
    ILoginService loginService;


    @RequestMapping("/studentLeaveExport")
    public void studentLeaveExport(LeaveVo leaveVo,HttpServletResponse response) throws IOException {

        // 添加查询条件，默认查询6个月类的信息
        if (Objects.isNull(leaveVo.getStartTime())){
            leaveVo.setStartTime(DateUtils.pastHalfYear());
        }

        List<LeaveVo> leaveVoList;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<String> roles = loginUser.getRoles();

        if (roles.contains(Constants.APPROVAL_ROLE_FD)){
            // 辅导员
            leaveVoList = leaveService.selectByFdy(leaveVo);
        }else if(roles.contains(Constants.APPROVAL_ROLE_SJ)){
            // 书记
            leaveVoList = leaveService.selectBySj(leaveVo);
        }else if(roles.contains(Constants.APPROVAL_ROLE_XSC)){
            // 学生处
            leaveVoList = leaveService.selectByXsc(leaveVo);
        }else {
            // 分管领导
            leaveVoList = leaveService.selectByFgld(leaveVo);
        }

//        List<LeaveVo> leaveVoList = leaveService.selectByFdy(leaveVo);

        List<StudentLeaveExportVo> studentLeaveExportVos = BeanCopyUtils.copyBeanList(leaveVoList, StudentLeaveExportVo.class);
        ExcelUtil.writeExcel(response,studentLeaveExportVos,"学生请假信息", StudentLeaveExportVo.class);
    }
}
